home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12212 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: hoxey.torolab.ibm.com!hoxey
  2. From: hoxey@hoxey.torolab.ibm.com ()
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Is This Bad Coding Practice?
  5. Date: 29 Mar 1996 20:05:02 GMT
  6. Organization: IBM Toronto
  7. Distribution: world
  8. Message-ID: <4jhfpe$15n2@tornews.torolab.ibm.com>
  9. References: <4jgnt2$9d1@loki.tor.hookup.net>
  10. Reply-To: hoxey@vnet.ibm.com
  11. NNTP-Posting-Host: hoxey.torolab.ibm.com
  12.  
  13. In article <4jgnt2$9d1@loki.tor.hookup.net>, Rajendra_Singh@msn.com (Rajendra Singh) writes:
  14. |> int main(int argc, char *argv[])
  15. |>    {
  16. |>    char string[128];
  17. |>    char *func1(void);
  18. |>
  19. |>    strcpy(string, func1());
  20. |>    return 0;
  21. |>    }
  22. |>
  23. |> char *func1(void)
  24. |>    {
  25. |>    char test[100];
  26. |>
  27. |>    sprintf(test, "Test:  %d", 1);
  28. |>    return test;
  29. |>    }
  30. |>
  31. |> ... since I am using the value returned from func1() immediately (in
  32. |> main()), is this reliable?  After I copy it into "string", I won't be
  33. Most definitely NOT!
  34.  
  35. |> using that area of memory anymore (i. e. the pointer returned by
  36. |> func1()).
  37. What if:
  38.    a) strcpy happens to be a *true* function (as opposed to a builtin) which
  39.       grabs some stack space for temporary use?... typically it would re-use
  40.       the space abandoned by func1()...
  41. OR b) immediately on exit from func1() your process loses use of the
  42.       processor due to an interrupt or something? Even if strcpy is a
  43.       builtin it is likely not an atomic operation... On some systems the
  44.       OS will use your stack at various points along the path to servicing
  45.       who know's what...
  46.  
  47. When func1() returns, the local 'char test[100];' is no more!
  48.  
  49. Steve
  50.